package com.mingqijia.gassafety.job.job.statistic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mingqijia.gassafety.db.entity.CmJoinInfo;
import com.mingqijia.gassafety.db.entity.CmStatDevice;
import com.mingqijia.gassafety.db.entity.auth.DictionaryItemEntry;
import com.mingqijia.gassafety.db.entity.vo.DeviceStatByDateVo;
import com.mingqijia.gassafety.db.entity.vo.SpConsumerVo;
import com.mingqijia.gassafety.db.entity.vo.TotalDeviceNumByDateVo;
import com.mingqijia.gassafety.db.mapper.CmJoinInfoMapper;
import com.mingqijia.gassafety.db.mapper.CmStatDeviceMapper;
import com.mingqijia.gassafety.db.mapper.ConsumerMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.db.mapper.auth.DictionaryItemMapper;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.shared.utils.RedisCacheExt;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: duanhongxiang
 * @Date: 2024/5/30 19:58
 */
@Configuration
@EnableScheduling
@Slf4j
public class JvJionInfoJob {
    @Autowired
    RedisCacheExt redisUtil;

    @Autowired
    CmJoinInfoMapper cmJoinInfoMapper;

    @Autowired
    ConsumerMapper consumerMapper;

    @XxlJob("dpJvJionInfo")
    //@Scheduled(cron = "* 0/1 * * * ?")
    public ReturnT<String> dpJvJionInfo(String param) {
        XxlJobLogger.log("XXLJOB-dpJvJionInfo start...");
        log.info("dpJvJionInfo--start--------");
        String key = Constants.CM_STAT_CONSUMER;
        try {
            Boolean acquire = redisUtil.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), Constants.TIME_OUT);//15分钟超时
            if (acquire) {
                try {
                    log.info("dpJvJionInfo--进入执行器--------");
                    if (execute()) {
                        return ReturnT.SUCCESS;
                    } else {
                        return ReturnT.FAIL;
                    }
                } catch (Exception e) {
                    log.info("dpJvJionInfo-error:{}", e);
                } finally {
                    redisUtil.del(key);
                }
            }
        } catch (Exception e) {
            log.info("dpJvJionInfo-error:{}", e);
        }

        log.info("dpJvJionInfo--end--------");
        XxlJobLogger.log("XXLJOB-dpJvJionInfo end...");
        return ReturnT.SUCCESS;
    }
    
    private boolean execute() {
        List<SpConsumerVo> list =  consumerMapper.getSpConsumerStat();
        if(ObjectUtils.isEmpty(list)){
            return true;
        }
        for(SpConsumerVo spConsumerVo : list){
            CmJoinInfo cmJoinInfo = new CmJoinInfo();
            cmJoinInfo.setGsscUserNums(spConsumerVo.getGsscUserNums());
            cmJoinInfo.setGsscUserNumM(spConsumerVo.getGsscUserNumM());
            cmJoinInfo.setGsscUserNumG(spConsumerVo.getGsscUserNumG());
            cmJoinInfo.setUpdateSource("1");
            cmJoinInfo.setId(spConsumerVo.getSpId());
            cmJoinInfo.setUpdateTime(new Date());
            cmJoinInfoMapper.updateById(cmJoinInfo);
        }
        return true;
    }
}
